package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocDispatchEmpLessAddDTO;
import org.jsola.hr.dto.SocDispatchEmpLessUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.SocDispatchEmpLessQuery;
import org.jsola.hr.service.ISocDispatchEmpLessService;
import org.jsola.hr.vo.SocDispatchEmpLessListVO;
import org.jsola.hr.vo.SocDispatchEmpLessVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;


/**
 * 社保派单员工表_减员
 *
 * @author zhr
 */
@RestController("hrSocDispatchEmpLessController")
@RequestMapping(HrConstants.API_PATH + "/v1/socDispatchEmpLesses")
public class SocDispatchEmpLessController implements ISocDispatchEmpLessControllerAPI {

    @Autowired
    private ISocDispatchEmpLessService socDispatchEmpLessService;

    @Override
    @PostMapping
    public Result<SocDispatchEmpLessVO> save(@Valid @RequestBody SocDispatchEmpLessAddDTO socDispatchEmpLessAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保派单员工表_减员权限校验

        return Result.success(
                socDispatchEmpLessService.save(socDispatchEmpLessAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocDispatchEmpLessUpdateDTO socDispatchEmpLessUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保派单员工表_减员权限校验

        int count = socDispatchEmpLessService.update(socDispatchEmpLessUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{socDispatchEmpLessId}")
    public Result<Integer> delete(@PathVariable String socDispatchEmpLessId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保派单员工表_减员权限校验

        int count = socDispatchEmpLessService.deleteByIds(tokenUser, socDispatchEmpLessId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socDispatchEmpLessIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保派单员工表_减员权限校验

        int count = socDispatchEmpLessService.deleteByIds(tokenUser, socDispatchEmpLessIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socDispatchEmpLessId}")
    public Result<SocDispatchEmpLessVO> findById(@PathVariable String socDispatchEmpLessId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单员工表_减员权限校验

        return Result.success(
            socDispatchEmpLessService.selectById(socDispatchEmpLessId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocDispatchEmpLessListVO>> listAll(@Valid SocDispatchEmpLessQuery socDispatchEmpLessQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单员工表_减员权限校验

        return Result.success(
            socDispatchEmpLessService.select(socDispatchEmpLessQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocDispatchEmpLessListVO>> page(@Valid SocDispatchEmpLessQuery socDispatchEmpLessQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socDispatchEmpLessService.selectPage(socDispatchEmpLessQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 临时保存到本地
     *
     * @param file 上传的文件
     * @return 临时文件
     */
    private File getInportExcel(MultipartFile file) {
        String excelFilePath = FileKit.saveFileToLocal(file, null, null, new String[]{"xls", "xlsx"}, null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return excelFile;
    }
}

